package br.com.moxeon.control.operation.rules;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import br.com.moxeon.model.Accounts;
import br.com.moxeon.model.cash.Throw;
import br.com.moxeon.model.cash.ThrowOperation;
import br.com.moxeon.model.enums.AccountSituation;
import br.com.moxeon.model.enums.TypeThrowItem;
import br.com.moxeon.model.enums.TypeThrowOperation;
import br.com.moxeon.model.enums.TypeThrowTransaction;
import br.com.moxeon.model.financialitems.CashierCheck;
import br.com.moxeon.model.financialitems.Check;
import br.com.moxeon.model.financialitems.DebitInAccount;
import br.com.moxeon.model.financialitems.FinancialItem;

@Service
@Scope("prototype")
public class PayableAccountDownloadRule extends FinancialRule{

	
	@SuppressWarnings("unchecked")
	@Override
	public void executeOperation() {
		super.executeOperation();
		Accounts payableAccount = (Accounts) getData().get("payableAccount");
		Double valueInDownload = (Double) getData().get("valueInDownload");
		Date payDate = (Date) getData().get("payDate");		
		List<FinancialItem> itensFinancial = (List<FinancialItem>) getData().get("itensFinancial");
		
		//TODO Validação
		
		getOperation().setTipoOperacao(TypeThrowOperation.BAIXA_DESPESA);
		payableAccount.setAccountSituation(AccountSituation.PAGA);
		payableAccount.setOperation(getOperation());
		payableAccount.setValueInDownload(valueInDownload); //Valor Pago /Recebido
		payableAccount.setPayDate(payDate);
		List<Accounts> accounts = new ArrayList<Accounts>();
		accounts.add(payableAccount);
		getOperation().setPayableAccounts(accounts);
		
		///TODO CLIENTE SOLUTIONS
		///TODO fazer maneira genérica de tratar lançamentos e passando por um controlador de cheques
		ArrayList<Throw> financialThrows = new ArrayList<Throw>();
		for (FinancialItem item : itensFinancial)
		{
			Throw trhow = new Throw();
			
			item.setFinancialThrow(trhow);
			if( item instanceof Check)
			{
				Check check = (Check) item;
				///definição da conta a ser feito o lançamento
				trhow.setTypeThrowItem(TypeThrowItem.CHEQUE);
				if (check instanceof CashierCheck)
				{
					trhow.setFinancialAccount(((CashierCheck) check).getBankAccount());
				}
				trhow.setThrowDate(null);
			}
			
			if( item instanceof DebitInAccount)
			{
				trhow.setFinancialAccount(((DebitInAccount) item).getAccount());
				trhow.setTypeThrowItem(TypeThrowItem.DEBITO);
				trhow.setThrowDate(((DebitInAccount) item).getDebitDay());//pega data do débito ou data de entrada do cheque
			}
			
			trhow.setActive(true);
			trhow.setOperation(getOperation());
			trhow.setType(TypeThrowTransaction.SAIDA);
			trhow.setValue(item.getItemValue());///getValue
			financialThrows.add(trhow);
			
		}
		
		
		getOperation().setListThorws(financialThrows);
		ArrayList<ThrowOperation> operations = new ArrayList<ThrowOperation>();
		operations.add(getOperation());
		finalizeOperation();
	}
	
	@Override
	public void reverseOperation() {
		
		
	}

}
